package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingDTO;
import com.itheima.service.OrderSettingService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "预约设置管理")
@RequiredArgsConstructor
public class OrderSettingController {
    private final OrderSettingService orderSettingService;
    private final AliOssUtil aliOssUtil;
    @GetMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        log.info("查询预约设置:{}", month);
        Result result = new Result();
        if (month != null && !month.isEmpty()) {
            month = month.matches("\\d{4}-\\d{1,2}") ?
                    month.replaceFirst("(\\d{4})-(\\d{1,2})", "$1-0$2") :
                    month;
        }
        try {
            List<OrderSetting> orderSetting =orderSettingService.getOrderSettingByMonth(month);
            result.setData(orderSetting);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.GET_ORDERSETTING_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.GET_ORDERSETTING_SUCCESS);
        return result;
    }

    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSettingDTO OrderSettingDTO){
        log.info("编辑预约设置:{}", OrderSettingDTO);
        Result result = new Result();
        try {
            orderSettingService.editNumberByOrderDate(OrderSettingDTO);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.ORDERSETTING_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.ORDERSETTING_SUCCESS);
        return result;
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile){
        log.info("上传预约设置:{}", excelFile);
        Result result = new Result();
        try {
            aliOssUtil.upload(excelFile.getBytes(), excelFile.getOriginalFilename());
            orderSettingService.importExcelData(excelFile);
            aliOssUtil.delete(excelFile.getOriginalFilename());
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.IMPORT_ORDERSETTING_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        return result;
    }
    @GetMapping("/download")
    public String download(String filename, HttpServletResponse response){
        log.info("下载预约设置");
            orderSettingService.download(filename,response);
   return "ok";
    }
}
